const http = require('../../../../../utils/http.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        chosen: '1',
        num: '20',
        nowNum: '1',
        nullData: {
            img: http.imgUrl + 'null_data_1.jpg',
            txt: '无此测试题记录'
        },
        has_miss: false
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 选择答案
     */
    chooseAnswer(e) {
        let id = e.currentTarget.dataset.id
        this.setData({
            chosen: id
        })
    },

    /**
     * 获取错题数据
     */
    getMytestMiss(e) {
        let that = this,
            url = 'Test_mytestMiss',
            token = wx.getStorageSync('token'),
            ltid = that.data.ltid,
            mstid = e ? http.dataIndex(e)[1] : '0'

        http._post(
            url,
            { token: token, ltid: ltid, mstid: mstid },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        queInfo: data.data,
                        has_miss: true
                    })
                } else {
                    if (e) {
                        wx.showToast({
                            title: '没有下一题了',
                            icon: 'none',
                            duration: 3000
                        })
                    }
                }
            },
            err => {
                console.log(err)
            }
        )

    },

    /**
     * 获取上一题题目信息
     */
    getMytestMissTop(e) {
        let that = this,
            url = 'Test_mytestMiss',
            token = wx.getStorageSync('token'),
            ltid = that.data.ltid,
            mstid = e ? http.dataIndex(e)[1] : '0'

        http._post(
            url,
            { token: token, ltid: ltid, mstid: mstid, top: 1 },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        queInfo: data.data,
                        has_miss: true
                    })
                } else {
                    if (e) {
                        wx.showToast({
                            title: '没有上一题了',
                            icon: 'none',
                            duration: 3000
                        })
                    }
                }
            },
            err => {
                console.log(err)
            }
        )

    },

    /**
     * 移除错题
     */
    delWrong(e) {
        let that = this,
            url = 'Test_delMymiss',
            token = wx.getStorageSync('token'),
            mstid = http.dataIndex(e)[1]

        http._post(
            url,
            { token: token, mstid: mstid },
            res => {
                console.log(res.data)

                let data = res.data
                wx.showToast({
                    title: data.info,
                    icon: 'none',
                    duration: 3000
                })
                if (data.re === '1') {
                    setTimeout(function () {
                        that.setData({
                            has_miss: false
                        })
                        that.getMytestMiss(e)
                    }, 2000)
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let ltid = options.ltid
        this.setData({
            ltid: ltid
        })

        this.getMytestMiss()  // 获取错题本错题
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})